# In make files we always define a rule in the following form:
# <output file>: <dependencies>
# TAB <command for producing output file from dependencies>

# In makefiles we have to use tabs for indentation. Otherwise we will get the
# error: "Makefile:...: *** missing separator.  Stop."

INCLUDE_DIR =./include
CC=gcc
CFLAGS=-I$(INCLUDE_DIR)
SOURCE_DIR=src
# define directory name for object files
OBJECT_DIR=src/obj
# local libraries are in a subdirectory called lib
LIB_DIR =./lib
# link math library
LIBS=-lm

# TODO: explain the following
# perhaps pathsubst is only for concattenating paths for an arbitrary number of files?
_DEPS = hellomake.h
DEPS = $(patsubst %,$(INCLUDE_DIR)/%,$(_DEPS))

# define object files locations
_OBJ = hellomake.o hellofunc.o
OBJ = $(patsubst %,$(OBJECT_DIR)/%,$(_OBJ))

# all object files depend on their .c files and all depend on DEPS
# $@ says to put the output of the compilation result to the file named on the left side of the ":"
# $< is the first item in the dependencies list
$(OBJECT_DIR)/%.o: $(SOURCE_DIR)/%.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

# hellomake consists of the object files, which according to the first rules
# depend on their corresponding .c files
# $@: the left hand side of the colon
# $^: the right hand side of the colon
hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

# declare clean as a rule, to prevent make from thinking it is on output file
.PHONY: clean

# clean cleans up files
clean:
	rm -f $(OBJECT_DIR)/*.o *~ core $(INCDIR)/*~
